// app.js
import { getLoginCode, getLoginToken, checkToken, checkSession } from './service/api_login'

App({
	globalData: {
		screenWidth: 0,
		screenHeight: 0,
		statusBarHeight: 0,
		navBarHeight: 44,
		deviceRadio: 0
	},

	onLaunch: async function () {
		// 1.获取了设备信息
		const info = wx.getSystemInfoSync()
		this.globalData.screenWidth = info.screenWidth
		this.globalData.screenHeight = info.screenHeight
		this.globalData.statusBarHeight = info.statusBarHeight

		const deviceRadio = info.screenHeight / info.screenWidth
		this.globalData.deviceRadio = deviceRadio

		const token = wx.getStorageSync('LOGIN_TOKEN')
		// 判断 token 是否过期
		const checkResult = await checkToken(token)
		console.log(checkResult)
		// 检查 session_key 是否过期
		const checkSessionResult = await checkSession()

		if (!token || checkResult.errorCode || !checkSessionResult) this.loginAction()
	},

	async loginAction() {
		// 获取登入的 code
		const { code } = await getLoginCode()

		// 将 code 传入服务器获取登入 token
		const { token } = await getLoginToken(code)
		wx.setStorageSync('LOGIN_TOKEN', token)
	}
})
